package com.cskaoyan.service.wxservice.impl;

import com.cskaoyan.bean.po.MarketCoupon;
import com.cskaoyan.bean.po.MarketCouponExample;
import com.cskaoyan.bean.po.MarketCouponUser;
import com.cskaoyan.bean.po.MarketCouponUserExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.service.wxservice.CouponWeChatService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 小黑球
 * @datetime 2023/1/1 14:55
 * @description: TODO:
 */
@Service
public class CouponWeChatServiceImpl implements CouponWeChatService {

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Override
    public CommonData<MarketCoupon> mylist(BasePageInfo info, Short status) {
        // 开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        // 排序
        MarketCouponExample example = new MarketCouponExample();
        if (info.getSort() != null) {
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }
        // 创建criteria拼接条件
        MarketCouponExample.Criteria criteria = example.createCriteria();
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketCoupon> list = marketCouponMapper.selectByExample(example);
        // 封装一个CommonData
        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketCoupon> list(BasePageInfo info) {
        // 开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        // 排序
        MarketCouponExample example = new MarketCouponExample();
        if (info.getSort() != null) {
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }
        MarketCouponExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketCoupon> list = marketCouponMapper.selectByExample(example);
        // 封装一个CommonData
        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /**
     * status的值为:
     * 0:表示正常使用;
     * 1:表示过期;
     * 2:表示下架;
     *
     * @param code
     * @return
     */
    @Override
    public int exchange(String code) {
        // 传过来的code跟数据库中的code做比较
        // 如果code存在,则兑换成功将status改为1
        // 根据code进行查询
        MarketCoupon marketCoupon = new MarketCoupon();
        // 对code赋值
        marketCoupon.setCode(code);
        // 拼接条件
        MarketCouponExample example = new MarketCouponExample();
        // 拼接多个条件
        MarketCouponExample.Criteria criteria = example.createCriteria();
        criteria.andCodeEqualTo(code);
        List<MarketCoupon> marketCouponsList = marketCouponMapper.selectByExample(example);
        if (marketCouponsList.size() == 0) {
            // 这个优惠券不存在
            return 742;
        } else if (marketCouponsList.size() == 1) {
            // 优惠券兑换码存在且唯一
            // 判断status的值
            Short status = marketCouponsList.get(0).getStatus();
            if (status == 0) {
                // 优惠卷可以正常使用
                // 可以兑换成功 status的值更改为1
                marketCouponsList.get(0).setStatus((short) 1);
                return 0;
            } else if (status == 1) {
                // 优惠券已过期
                return 742;
            } else if (status == 2) {
                // 优惠卷已下架
                return 740;
            }
        }
        return 404;
    }

    @Override
    public int receive(Integer couponId) {
        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setCouponId(couponId);
        MarketCouponUserExample example = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andCouponIdEqualTo(couponId);
        List<MarketCouponUser> list = marketCouponUserMapper.selectByExample(example);
        if (list.size() == 0) {
            // 代码执行到这里证明根据couponId查询到优惠券
            // 判断status的值来确定能否领取成功
            // status的值为0时
            return 0;
        }
        return 740;
    }

    @Override
    public CommonData<MarketCoupon> selectList(Integer cartId, Integer grouponResultId) {
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        MarketCoupon marketCoupon = new MarketCoupon();
//        marketCoupon.getDesc();
//        marketCoupon.setDiscount();
//        marketCoupon.setEndTime();
//        marketCoupon.setId();
//        marketCoupon.setMin();
//        marketCoupon.setName();
//        marketCoupon.setStartTime();
//        marketCoupon.setTag();

        if (cartId != 0) {
        }
        if (grouponResultId != 0) {

        }
        return null;
    }
}
